找传奇、传世资源到传世资源站!

ocr 文字识别 点阵 示例源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

点阵获取字模,匹配识别图片字体from clipboard
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Data.OleDb;namespace OCR{ struct Suited { public string Character; //字符名 public int MaxMatch; //匹配率 public float MaxUseCount; //使用频率 public Suited(string Character, int MaxMatch, float MaxUseCount) { this.Character = Character; this.MaxMatch = MaxMatch; this.MaxUseCount = MaxUseCount; } } /// <summary> /// OCR 的摘要说明。 /// </summary> public class FormMain : System.Windows.Forms.Form { private System.Windows.Forms.Label TeachLabelText; private System.Windows.Forms.TextBox inputLearnCharacter; private System.Windows.Forms.Button buttonLearnConfirm; private System.Windows.Forms.Button buttonLearnCancel; private System.Windows.Forms.Label textResult; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; string strCaption = ""; bool currentlyDrawing = false; Bitmap userDrawAreaBmp; Bitmap userTemplateAreaBmp; ArrayList arrCurrentEigenvalue; ArrayList resultList; OleDbConnection DbConn; OleDbCommand DbCmd; public int SplitWidth = 20; public int SplitHeight = 20; public Size PointSize = new Size(10, 10); private System.Windows.Forms.GroupBox frameArea1; private System.Windows.Forms.GroupBox frameArea3; private System.Windows.Forms.PictureBox userDrawArea; private System.Windows.Forms.Button buttonRecognise; private System.Windows.Forms.Button buttonClearScreen; private System.Windows.Forms.PictureBox userTemplateArea; private System.Windows.Forms.ListBox ResultList; private System.Windows.Forms.ProgressBar progressRecognition; private System.Windows.Forms.Label progressText; private System.Windows.Forms.Label resultText; private System.Windows.Forms.TrackBar tbThreshold; private System.Windows.Forms.Button AutoLearn; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.Button btnBreak; private TextBox tbG; private Button button1; private Button button2; private System.Windows.Forms.Label precisionText; public FormMain() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.TeachLabelText = new System.Windows.Forms.Label(); this.inputLearnCharacter = new System.Windows.Forms.TextBox(); this.buttonLearnConfirm = new System.Windows.Forms.Button(); this.buttonLearnCancel = new System.Windows.Forms.Button(); this.textResult = new System.Windows.Forms.Label(); this.frameArea1 = new System.Windows.Forms.GroupBox(); this.button2 = new System.Windows.Forms.Button(); this.tbG = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.buttonRecognise = new System.Windows.Forms.Button(); this.buttonClearScreen = new System.Windows.Forms.Button(); this.userDrawArea = new System.Windows.Forms.PictureBox(); this.frameArea3 = new System.Windows.Forms.GroupBox(); this.btnBreak = new System.Windows.Forms.Button(); this.AutoLearn = new System.Windows.Forms.Button(); this.userTemplateArea = new System.Windows.Forms.PictureBox(); this.ResultList = new System.Windows.Forms.ListBox(); this.progressRecognition = new System.Windows.Forms.ProgressBar(); this.progressText = new System.Windows.Forms.Label(); this.resultText = new System.Windows.Forms.Label(); this.tbThreshold = new System.Windows.Forms.TrackBar(); this.precisionText = new System.Windows.Forms.Label(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.frameArea1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.userDrawArea)).BeginInit(); this.frameArea3.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.userTemplateArea)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.tbThreshold)).BeginInit(); this.SuspendLayout(); // // TeachLabelText // this.TeachLabelText.AutoSize = true; this.TeachLabelText.Location = new System.Drawing.Point(13, 420); this.TeachLabelText.Name = "TeachLabelText"; this.TeachLabelText.Size = new System.Drawing.Size(161, 18); this.TeachLabelText.TabIndex = 7; this.TeachLabelText.Text = "请输入学习的字符:"; // // inputLearnCharacter // this.inputLearnCharacter.Location = new System.Drawing.Point(13, 456); this.inputLearnCharacter.MaxLength = 1; this.inputLearnCharacter.Name = "inputLearnCharacter"; this.inputLearnCharacter.Size = new System.Drawing.Size(40, 28); this.inputLearnCharacter.TabIndex = 8; // // buttonLearnConfirm // this.buttonLearnConfirm.Location = new System.Drawing.Point(67, 456); this.buttonLearnConfirm.Name = "buttonLearnConfirm"; this.buttonLearnConfirm.Size = new System.Drawing.Size(66, 34); this.buttonLearnConfirm.TabIndex = 9; this.buttonLearnConfirm.Text = "确认"; this.buttonLearnConfirm.Click = new System.EventHandler(this.buttonLearnConfirm_Click); // // buttonLearnCancel // this.buttonLearnCancel.Location = new System.Drawing.Point(133, 456); this.buttonLearnCancel.Name = "buttonLearnCancel"; this.buttonLearnCancel.Size = new System.Drawing.Size(67, 34); this.buttonLearnCancel.TabIndex = 10; this.buttonLearnCancel.Text = "取消"; this.buttonLearnCancel.Click = new System.EventHandler(this.buttonLearnCancel_Click); // // textResult // this.textResult.AutoSize = true; this.textResult.Location = new System.Drawing.Point(13, 516); this.textResult.Name = "textResult"; this.textResult.Size = new System.Drawing.Size(143, 18); this.textResult.TabIndex = 11; this.textResult.Text = "字符识别状态..."; // // frameArea1 // this.frameArea1.Controls.Add(this.button2); this.frameArea1.Controls.Add(this.tbG); this.frameArea1.Controls.Add(this.button1); this.frameArea1.Controls.Add(this.buttonRecognise); this.frameArea1.Controls.Add(this.buttonClearScreen); this.frameArea1.Controls.Add(this.userDrawArea); this.frameArea1.Location = new System.Drawing.Point(13, 12); this.frameArea1.Name = "frameArea1"; this.frameArea1.Size = new System.Drawing.Size(360, 396); this.frameArea1.TabIndex = 12; this.frameArea1.TabStop = false; this.frameArea1.Text = "用户绘制区域"; // // button2 // this.button2.Location = new System.Drawing.Point(313, 343); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(34, 47); this.button2.TabIndex = 9; this.button2.Text = "图片"; this.button2.UseVisualStyleBackColor = true; this.button2.Click = new System.EventHandler(this.button2_Click); // // tbG // this.tbG.Location = new System.Drawing.Point(175, 353); this.tbG.MaxLength = 1; this.tbG.Name = "tbG"; this.tbG.Size = new System.Drawing.Size(91, 28); this.tbG.TabIndex = 8; // // button1 // this.button1.Location = new System.Drawing.Point(272, 342); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(34, 48); this.button1.TabIndex = 7; this.button1.Text = "生成"; this.button1.UseVisualStyleBackColor = true; this.button1.Click = new System.EventHandler(this.button1_Click); // // buttonRecognise // this.buttonRecognise.Location = new System.Drawing.Point(88, 348); this.buttonRecognise.Name = "buttonRecognise"; this.buttonRecognise.Size = new System.Drawing.Size(73, 34); this.buttonRecognise.TabIndex = 6; this.buttonRecognise.Text = "识别"; this.buttonRecognise.Click = new System.EventHandler(this.buttonRecognise_Click); // // buttonClearScreen // this.buttonClearScreen.Location = new System.Drawing.Point(13, 348); this.buttonClearScreen.Name = "buttonClearScreen"; this.buttonClearScreen.Size = new System.Drawing.Size(69, 34); this.buttonClearScreen.TabIndex = 5; this.buttonClearScreen.Text = "清除"; this.buttonClearScreen.Click = new System.EventHandler(this.buttonClearScreen_Click); // // userDrawArea // this.userDrawArea.BackColor = System.Drawing.Color.White; this.userDrawArea.Location = new System.Drawing.Point(13, 36); this.userDrawArea.Name = "userDrawArea"; this.userDrawArea.Size = new System.Drawing.Size(334, 300); this.userDrawArea.TabIndex = 1; this.userDrawArea.TabStop = false; this.userDrawArea.MouseDown = new System.Windows.Forms.MouseEventHandler(this.userDrawArea_MouseDown); this.userDrawArea.MouseMove = new System.Windows.Forms.MouseEventHandler(this.userDrawArea_MouseMove); this.userDrawArea.MouseUp = new System.Windows.Forms.MouseEventHandler(this.userDrawArea_MouseUp); // // frameArea3 // this.frameArea3.Controls.Add(this.btnBreak); this.frameArea3.Controls.Add(this.AutoLearn); this.frameArea3.Controls.Add(this.userTemplateArea); this.frameArea3.Location = new System.Drawing.Point(387, 12); this.frameArea3.Name = "frameArea3"; this.frameArea3.Size = new System.Drawing.Size(360, 396); this.frameArea3.TabIndex = 14; this.frameArea3.TabStop = false; this.frameArea3.Text = "区域分析"; // // btnBreak // this.btnBreak.Location = new System.Drawing.Point(187, 348); this.btnBreak.Name = "btnBreak"; this.btnBreak.Size = new System.Drawing.Size(160, 34); this.btnBreak.TabIndex = 5; this.btnBreak.Text = "中断学习"; this.btnBreak.Click = new System.EventHandler(this.btnBreak_Click); // // AutoLearn // this.AutoLearn.Location = new System.Drawing.Point(13, 348); this.AutoLearn.Name = "AutoLearn"; this.AutoLearn.Size = new System.Drawing.Size(160, 34); this.AutoLearn.TabIndex = 4; this.AutoLearn.Text = "自动学习"; this.AutoLearn.Click = new System.EventHandler(this.AutoLearn_Click); // // userTemplateArea // this.userTemplateArea.BackColor = System.Drawing.Color.White; this.userTemplateArea.Location = new System.Drawing.Point(13, 36); this.userTemplateArea.Name = "userTemplateArea"; this.userTemplateArea.Size = new System.Drawing.Size(334, 300); this.userTemplateArea.TabIndex = 3; this.userTemplateArea.TabStop = false; // // ResultList // this.ResultList.ColumnWidth = 20; this.ResultList.HorizontalScrollbar = true; this.ResultList.ItemHeight = 18; this.ResultList.Location = new System.Drawing.Point(388, 416); this.ResultList.MultiColumn = true; this.ResultList.Name = "ResultList"; this.ResultList.Size = new System.Drawing.Size(333, 40); this.ResultList.TabIndex = 15; this.ResultList.DoubleClick = new System.EventHandler(this.ResultList_DoubleClick); // // progressRecognition // this.progressRecognition.Location = new System.Drawing.Point(387, 462); this.progressRecognition.Name = "progressRecognition"; this.progressRecognition.Size = new System.Drawing.Size(333, 34); this.progressRecognition.TabIndex = 16; // // progressText // this.progressText.AutoSize = true; this.progressText.Location = new System.Drawing.Point(293, 468); this.progressText.Name = "progressText"; this.progressText.Size = new System.Drawing.Size(89, 18); this.progressText.TabIndex = 17; this.progressText.Text = "处理进度:"; // // resultText // this.resultText.AutoSize = true; this.resultText.Location = new System.Drawing.Point(293, 420); this.resultText.Name = "resultText"; this.resultText.Size = new System.Drawing.Size(89, 18); this.resultText.TabIndex = 18; this.resultText.Text = "可选文字:"; // // tbThreshold // this.tbThreshold.Location = new System.Drawing.Point(388, 502); this.tbThreshold.Maximum = 400; this.tbThreshold.Minimum = 1; this.tbThreshold.Name = "tbThreshold"; this.tbThreshold.Size = new System.Drawing.Size(332, 69); this.tbThreshold.TabIndex = 100; this.tbThreshold.TickFrequency = 10; this.tbThreshold.TickStyle = System.Windows.Forms.TickStyle.None; this.tbThreshold.Value = 200; // // precisionText // this.precisionText.AutoSize = true; this.precisionText.Location = new System.Drawing.Point(293, 516); this.precisionText.Name = "precisionText"; this.precisionText.Size = new System.Drawing.Size(89, 18); this.precisionText.TabIndex = 20; this.precisionText.Text = "识别精度:"; // // openFileDialog1 // this.openFileDialog1.Filter = "文本文件|*.txt"; this.openFileDialog1.Title = "请选择源文件!用于学习!"; // // FormMain // this.AutoScaleBaseSize = new System.Drawing.Size(10, 21); this.ClientSize = new System.Drawing.Size(758, 550); this.Controls.Add(this.precisionText); this.Controls.Add(this.tbThreshold); this.Controls.Add(this.resultText); this.Controls.Add(this.progressText); this.Controls.Add(this.progressRecognition); this.Controls.Add(this.ResultList); this.Controls.Add(this.frameArea3); this.Controls.Add(this.frameArea1); this.Controls.Add(this.textResult); this.Controls.Add(this.buttonLearnCancel); this.Controls.Add(this.buttonLearnConfirm); this.Controls.Add(this.inputLearnCharacter); this.Controls.Add(this.TeachLabelText); this.Name = "FormMain"; this.Text = "OCR 文字识别"; this.Load = new System.EventHandler(this.FormMain_Load); this.frameArea1.ResumeLayout(false); this.frameArea1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.userDrawArea)).EndInit(); this.frameArea3.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.userTemplateArea)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.tbThreshold)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new FormMain()); } private void ClearGraphics(Image img, Color clearColor) { if (img != null) { Graphics g = Graphics.FromImage(img); g.FillRectangle(new SolidBrush(clearColor), 0, 0, img.Width, img.Height); g.Dispose(); } userDrawArea.Refresh(); userTemplateArea.Refresh(); } private void buttonLearnCancel_Click(object sender, System.EventArgs e) { this.inputLearnCharacter.Text = ""; } private void buttonClearScreen_Click(object sender, System.EventArgs e) { arrCurrentEigenvalue.Clear(); arrCurrentEigenvalue.TrimToSize(); this.textResult.Text = ""; this.progressRecognition.Value = 0; ClearGraphics(userDrawArea.Image, Color.White); ClearGraphics(userTemplateArea.Image, Color.White); } private void GraspRawData() { bool bool1stScan = true; int ax, ay, bx, by; this.ClearGraphics(this.userTemplateArea.Image, Color.White); ax = ay = bx = by = 0; Graphics g = Graphics.FromImage(userTemplateAreaBmp); int b = 0; for (int i = 0; i < userDrawAreaBmp.Width; i = (userDrawAreaBmp.Width / this.SplitWidth) / 2) for (int j = 0; j < userDrawAreaBmp.Height; j = (userDrawAreaBmp.Height / this.SplitHeight) / 2) { //if (userDrawAreaBmp.GetPixel(i, j).ToArgb() == Color.Black.ToArgb()) if (userDrawAreaBmp.GetPixel(i, j).B < 100) { if (!bool1stScan) { if (i < ax) ax = i; if (i >= bx) bx = i; if (j < ay) ay = j; if (j >= by) by = j; } else { bool1stScan = false; ax = i; bx = i; ay = j; by = j; } } b ; } this.arrCurrentEigenvalue.Clear(); this.arrCurrentEigenvalue.TrimToSize(); if ((bx - ax) != 0 && (by - ay) != 0 && (((bx - ax) > (userDrawAreaBmp.Width / this.SplitWidth) / 2) || ((by - ay) > (userDrawAreaBmp.Height / this.SplitHeight) / 2))) { this.ClearGraphics(this.userTemplateArea.Image, Color.White); Bitmap memBmp = new Bitmap(userTemplateAreaBmp.Width, userTemplateAreaBmp.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); Graphics memGp = Graphics.FromImage(memBmp); memGp.Clear(Color.White); memGp.DrawImage(userDrawAreaBmp, new Rectangle(0, 0, memBmp.Width - 1, memBmp.Height - 1), ax, ay, bx - ax, by - ay, GraphicsUnit.Pixel); int pw = (userDrawAreaBmp.Width / this.SplitWidth); int ph = (userDrawAreaBmp.Height / this.SplitHeight); for (int i = 0; i < memBmp.Width; i = (memBmp.Width / this.SplitWidth)) { for (int j = 0; j < memBmp.Height; j = (memBmp.Height / this.SplitHeight)) { //if (memBmp.GetPixel(i, j).ToArgb() == Color.Black.ToArgb()) if (memBmp.GetPixel(i, j).B < 100) { g.FillRectangle(new SolidBrush(Color.Black), i - (pw / 2), j - (ph / 2), pw, ph); g.DrawRectangle(new Pen(Color.White, 1f), i - (pw / 2), j - (ph / 2), pw, ph); arrCurrentEigenvalue.Add("1"); } else { arrCurrentEigenvalue.Add("0"); } } } memGp.Dispose(); memBmp.Dispose(); } //textResult.Text = this.arrCurrentEigenvalue.Count.ToString(); g.Dispose(); userTemplateArea.Refresh(); } /// <summary> /// 保存学习结果 /// </summary> private void SaveLearn(string LearnCharacter) { if (arrCurrentEigenvalue.Count <= 0) { return; } //建立特征码 string Eigenvalue = ""; foreach (string ce in this.arrCurrentEigenvalue) Eigenvalue = ce; try { DbCmd.CommandText = string.Format("select Count(*) as exp1 from tbLearn where LearnCharacter = '{0}'", LearnCharacter.Replace("'", "''")); OleDbDataReader ddr = DbCmd.ExecuteReader(); bool isUpdate = false; while (ddr.Read()) { if (ddr.GetInt32(0) != 0) { //更新 isUpdate = true; break; } } ddr.Close(); if (isUpdate) DbCmd.CommandText = string.Format("update tbLearn set Eigenvalue='{0}' where LearnCharacter = '{1}'", Eigenvalue, LearnCharacter.Replace("'", "''")); else DbCmd.CommandText = string.Format("insert into tbLearn (LearnCharacter,Eigenvalue,Frequency)values('{0}','{1}',0)", LearnCharacter.Replace("'", "''"), Eigenvalue); DbCmd.ExecuteNonQuery(); } catch (Exception ex) { this.strCaption = ex.Message; } finally { this.textResult.Text = strCaption; } } private void buttonLearnConfirm_Click(object sender, System.EventArgs e) { if (this.inputLearnCharacter.Text.Length == 0) return; SaveLearn(this.inputLearnCharacter.Text); } private void userDrawArea_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { this.currentlyDrawing = true; userDrawArea_MouseMove(sender, e); } private void userDrawArea_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (this.currentlyDrawing) { Graphics g = Graphics.FromImage(userDrawAreaBmp); g.FillPie(new SolidBrush(Color.Black), e.X - (PointSize.Width / 2), e.Y - (PointSize.Height / 2), PointSize.Width, PointSize.Height, 0f, 360f); g.Dispose(); userDrawArea.Refresh(); } } private void userDrawArea_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { this.currentlyDrawing = false; this.GraspRawData(); } private void FormMain_Load(object sender, System.EventArgs e) { Image userDrawAreaImage = userDrawAreaBmp = new Bitmap(200, 200, System.Drawing.Imaging.PixelFormat.Format24bppRgb); using (Graphics g = Graphics.FromImage(userDrawAreaImage)) { g.Clear(Color.White); } Image userTemplateAreaImage = userTemplateAreaBmp = new Bitmap(200, 200, System.Drawing.Imaging.PixelFormat.Format24bppRgb); using (Graphics g = Graphics.FromImage(userTemplateAreaImage)) { g.Clear(Color.White); } this.userDrawArea.Image = userDrawAreaImage; this.userTemplateArea.Image = userTemplateAreaImage; //PointSize = new Size(10,10); arrCurrentEigenvalue = new ArrayList(this.SplitWidth * this.SplitHeight); resultList = new ArrayList(); DbConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) @"\Learn.mdb;"); DbCmd = new OleDbCommand(); try { DbConn.Open(); DbCmd.Connection = this.DbConn; } catch (Exception ex) { MessageBox.Show(ex.Message); Application.Exit(); } } private void buttonRecognise_Click(object sender, System.EventArgs e) { //识别 DbCmd.CommandText = "select LearnCharacter,Eigenvalue,Frequency from tbLearn order by Frequency"; OleDbDataReader ddr = DbCmd.ExecuteReader(); int maxMatch = 0; progressRecognition.Value = 0; progressRecognition.Maximum = ddr.GetSchemaTable().Rows.Count; progressRecognition.Minimum = 0; resultList.Clear(); resultList.TrimToSize(); while (ddr.Read()) { //比较 if (!ddr.IsDBNull(1)) maxMatch = Match(ddr.GetString(1)); //MessageBox.Show (maxMatch.ToString()); //保留匹配率字符的精度,建议10% if (maxMatch >= tbThreshold.Value) { Suited suited = new Suited(ddr.GetString(0), maxMatch, ddr.GetFloat(2)); resultList.Add(suited); } //找出匹配率最高的 resultList.Sort(0, resultList.Count, new MaxMatchSort()); //保存100个字符 //while(resultList.Count>(this.SplitWidth * this.SplitHeight)) //{ //resultList.RemoveAt(resultList.Count-1); //} if (progressRecognition.Value < progressRecognition.Maximum) progressRecognition.Value = 1; } ddr.Close(); //分离结果 //找出频率最高的字符 resultList.Sort(0, resultList.Count, new MaxUseCountSort()); //保存匹配率最高的10个字符 while (resultList.Count > 10) { resultList.RemoveAt(resultList.Count - 1); } //找出匹配率最高的 resultList.Sort(0, resultList.Count, new MaxMatchSort()); ResultList.Items.Clear(); foreach (Suited s in resultList) { ResultList.Items.Add(s.Character); } resultList.Clear(); resultList.TrimToSize(); progressRecognition.Value = progressRecognition.Maximum; } private int Match(string Eigenvalue) { int maxMatch = 0; string c = ""; string e = ""; int d = 0; if (Eigenvalue.Length < (this.SplitWidth * this.SplitHeight)) return 0; for (int i = 0; i < this.SplitWidth; i ) { for (int j = 0; j < this.SplitHeight; j ) { if (d >= this.arrCurrentEigenvalue.Count) return maxMatch; c = Eigenvalue.Substring(d, 1); e = (string)this.arrCurrentEigenvalue[d]; if (c == "1") { if (e == "1") maxMatch ; else maxMatch--; } else { if (e == "0") maxMatch ; else maxMatch--; } d ; } } return maxMatch; } private void ResultList_DoubleClick(object sender, System.EventArgs e) { string LearnCharacter = (string)ResultList.SelectedItem ""; if (LearnCharacter != "") { DbCmd.CommandText = string.Format("update tbLearn set Frequency=Frequency 0.01 where LearnCharacter = '{0}'", LearnCharacter.Replace("'", "''")); DbCmd.ExecuteNonQuery(); MessageBox.Show(LearnCharacter); } } private void AutoLearn_Click(object sender, System.EventArgs e) { if (openFileDialog1.ShowDialog(this) == DialogResult.OK) { //开始分析 System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName, System.Text.Encoding.Default); string allText = sr.ReadToEnd(); string c = ""; sr.Close(); allText = allText.Replace(" ", ""); allText = allText.Replace("\r", ""); allText = allText.Replace("\n", ""); allText = allText.Replace("\t", ""); Bitmap memBmp = new Bitmap(this.userDrawAreaBmp.Width, this.userDrawAreaBmp.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); Graphics memGp = Graphics.FromImage(memBmp); Graphics uGp = Graphics.FromImage(userDrawAreaBmp); progressRecognition.Value = 0; progressRecognition.Minimum = 0; progressRecognition.Maximum = allText.Length - 1; btnBreak.Tag = 0; for (int i = 0; i < allText.Length; i ) { c = allText.Substring(i, 1); memGp.Clear(Color.White); //memGp.FillRectangle(new SolidBrush(Color.White), 0, 0, memBmp.Width, memBmp.Height); memGp.DrawString(c, new Font("宋体", 100f, FontStyle.Bold), new SolidBrush(Color.Black), 0, 0); //memGp.DrawString(c, new Font("楷体_GB2312", 100f, FontStyle.Bold), new SolidBrush(Color.Black), 0, 0); uGp.Clear(Color.White); //uGp.FillRectangle(new SolidBrush(Color.White), 0, 0, userDrawAreaBmp.Width, userDrawAreaBmp.Height); uGp.DrawImage(memBmp, new Rectangle(0, 0, userDrawAreaBmp.Width, userDrawAreaBmp.Height), 0, 0, memBmp.Width, memBmp.Height, System.Drawing.GraphicsUnit.Pixel); //分析特征码 GraspRawData(); //保存特征码 SaveLearn(c); textResult.Text = "正在自学习:" c; try { textResult.Refresh(); } catch {; } if (progressRecognition.Value < progressRecognition.Maximum) progressRecognition.Value = 1; if ((int)btnBreak.Tag == 1) break; Application.DoEvents(); } uGp.Dispose(); memGp.Dispose(); memBmp.Dispose(); MessageBox.Show("自学习完毕!"); } } private void btnBreak_Click(object sender, System.EventArgs e) { btnBreak.Tag = 1; } private void button1_Click(object sender, EventArgs e) { Bitmap memBmp = new Bitmap(this.userDrawAreaBmp.Width, this.userDrawAreaBmp.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); Graphics memGp = Graphics.FromImage(memBmp); memGp.Clear(Color.White); Graphics uGp = Graphics.FromImage(userDrawAreaBmp); memGp.Clear(Color.White); //memGp.FillRectangle(new SolidBrush(Color.White), 0, 0, memBmp.Width, memBmp.Height); memGp.DrawString(tbG.Text, new Font("宋体", 100f, FontStyle.Bold), new SolidBrush(Color.Black), 0, 0); //memGp.DrawString(c, new Font("楷体_GB2312", 100f, FontStyle.Bold), new SolidBrush(Color.Black), 0, 0); uGp.Clear(Color.White); //uGp.FillRectangle(new SolidBrush(Color.White), 0, 0, userDrawAreaBmp.Width, userDrawAreaBmp.Height); uGp.DrawImage(memBmp, new Rectangle(0, 0, userDrawAreaBmp.Width, userDrawAreaBmp.Height), 0, 0, memBmp.Width, memBmp.Height, System.Drawing.GraphicsUnit.Pixel); //分析特征码 GraspRawData(); uGp.Dispose(); memGp.Dispose(); memBmp.Dispose(); } private void button2_Click(object sender, EventArgs e) { OpenFileDialog opnDlg = new OpenFileDialog(); opnDlg.Filter = "所有图像文件 | *.bmp; *.pcx; *.png; *.jpg; *.gif;" "*.tif; *.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf|" "位图( *.bmp; *.jpg; *.png;...) | *.bmp; *.pcx; *.png; *.jpg; *.gif; *.tif; *.ico|" "矢量图( *.wmf; *.eps; *.emf;...) | *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf"; opnDlg.Title = "打开图像文件"; opnDlg.ShowHelp = true; if (opnDlg.ShowDialog() == DialogResult.OK) { var imgPath = opnDlg.FileName; try { var img = Image.FromFile(imgPath); using (Graphics uGp = Graphics.FromImage(userDrawAreaBmp)) { uGp.DrawImage(img, 0, 0, userDrawAreaBmp.Width, userDrawAreaBmp.Height); } GraspRawData(); } catch (Exception exp) { MessageBox.Show(exp.Message); } } } //private int BinToDec(string strBin) //{ //int i = strBin.Length; //int btd = 0; //while(i>0) //{ //if(strBin.Substring(i,1)=="1") //btd = btd (int)Math.Pow(2,strBin.Length-i); //i -- ; //} //return btd; //} // //private string DecToBin(int intDec,int intDigit) //{ //int intTemp = 0; //string dtb = ""; //while(intDec>0 && intDigit>0) //{ //intDigit--; //intTemp = intDec % 2; //if(intTemp!=0) //{ //dtb = "1" dtb; //intDec = (intDec-1) /2; //} //else //{ //dtb = "0" dtb; //intDec = intDec / 2; //} //} // //while(intDigit!=0) //{ //intDigit --; //dtb = "0" dtb; //} //return dtb; //} } public class MaxMatchSort : IComparer { int IComparer.Compare(Object x, Object y) { Suited sx = (Suited)x; Suited sy = (Suited)y; CaseInsensitiveComparer cic = new CaseInsensitiveComparer(); return cic.Compare(sy.MaxMatch, sx.MaxMatch); } } public class MaxUseCountSort : IComparer { int IComparer.Compare(Object x, Object y) { Suited sx = (Suited)x; Suited sy = (Suited)y; CaseInsensitiveComparer cic = new CaseInsensitiveComparer(); return cic.Compare(sy.MaxUseCount, sx.MaxUseCount); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复